#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@file: obs_gk_video_recording.py
@author: lyndon
@time: Created on 2022-02-19 20:07
@env: Python
@desc:
@ref:
@blog:
"""
import os
import json
import sys
import time
import logging
import html2text
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
logging.basicConfig(level=logging.INFO)


sys.path.append('../')
from obswebsocket import obsws, requests  # noqa: E402

BIG_PLAY_CLASS = "ColumnArticleMiniAudioPlayer_icon_2Kms_"
SMALL_PLAY_CLASS = "play"
ARTICAL_TITLE_CLASS = "Index_articleTitle_Elbu-"
NEXT_PAGE_CLASS = "Index_btn_1b3jQ"
IS_OBS_RECORD = True
IS_PAGE_PLAY = True

# browser_path = r"D:\iSoft\chromedriver_win32"
browser_driver = "chromedriver.exe"
# browser_driver = "geckodriver.exe"
# browser_driver_path = os.path.join(browser_path, browser_driver)

# globel_out_path = r"f:\Learn\rust-base\rust第一课-陈天"
# globel_out_path = r"f:\Learn\greek_time\kubernetes"
globel_out_path = r"d:\ETL\DATA\test"


max_retry_time = 3


def article_response(browser, article_title):
    request_log = browser.get_log('performance')
    for i in range(len(request_log)):
        message = json.loads(request_log[i]['message'])
        message = message['message']['params']
        # .get() 方式获取是了避免字段不存在时报错
        request = message.get('request')
        if (request is None):
            continue

        url = request.get('url')
        # print(url)
        if url == "https://apptime.geekbang.org/api/time/serv/v1/article":
            print(url)
            # 得到requestId
            # 通过requestId获取接口内容
            try:
                content = browser.execute_cdp_cmd('Network.getResponseBody', {'requestId': message['requestId']})
            except Exception as e:
                time.sleep(3)
                print(f"get article request id failed:{e}")
                continue
            if content is not None and "body" in content:
                body = content["body"]
                body = json.loads(body)
                if "data" in body:
                    resp_data = body["data"]
                    if "article_content" in resp_data:
                        outfile = os.path.join(globel_out_path, f"{article_title}.md")
                        with open(outfile, mode='w+', encoding='utf-8') as f:
                            f.write(f"# {article_title}\n")
                            f.write(html2text.html2text(resp_data["article_content"]))
            # time.sleep(6)
            continue

        if url == "https://apptime.geekbang.org/api/time/serv/v1/comments":
            # print(url)
            # 得到requestId
            # 通过requestId获取接口内容
            try:
                content = browser.execute_cdp_cmd('Network.getResponseBody', {'requestId': message['requestId']})
            except Exception as e:
                time.sleep(3)
                print(f"get comments request id failed:{e}")
                continue
            if content and "body" in content:
                body = content["body"]
                body = json.loads(body)
                if "data" in body:
                    resp_data = body["data"]
                    outfile = os.path.join(globel_out_path, f"{article_title}.md")
                    with open(outfile, mode='a+', encoding='utf-8') as f:
                        f.write("## 精选留言\n")
                        if "list" in resp_data:
                            comments = resp_data["list"]
                            for cmts in comments:
                                cc = html2text.html2text(cmts["comment_content"])
                                f.write(f"\n{cc}\n")
                                if "replies" in cmts:
                                    for reply in cmts["replies"]:
                                        replay_msg = reply["content"]
                                        f.write(f"```log\n{html2text.html2text(replay_msg)}```\n\n\n")
            continue


def get_title_from_page(browser):
    article_title = ""
    while True:
        try:
            title = browser.find_element(By.CLASS_NAME, ARTICAL_TITLE_CLASS)
            article_title = title.get_attribute('textContent')
            article_title = article_title.replace(' ', '')
            article_title = article_title.replace('|', '-')
            article_title = article_title.replace('/', '-')
            article_title = article_title.replace('[', '（')
            article_title = article_title.replace(']', '）')
            article_title = article_title.replace('<', '《')
            article_title = article_title.replace('>', '》')
            article_title = article_title.replace('&', '-')
            break
        except Exception as e:
            print(f"get article title failed.")
            time.sleep(5)
            continue
    return article_title


class TypeMappingError(BaseException):
    def __init__(self, ErrorInfo):
        # super().__init__(self)  # 初始化父类
        self.message = ErrorInfo

    def __str__(self):
        return self.message


def find_and_play(browser):
    if IS_PAGE_PLAY is False:
        return
    while True:
        try:
            w = browser.find_element(By.CLASS_NAME, BIG_PLAY_CLASS)  # 寻找新页面播放按钮
            if w.is_enabled():
                # time.sleep(10)
                w.click()
                break
        except Exception as e:
            time.sleep(2)
            print(f"find and play error")
            continue
        else:
            # time.sleep(2)
            continue


if __name__ == '__main__':
    caps = DesiredCapabilities.CHROME
    # as per latest docs
    caps['goog:loggingPrefs'] = {'performance': 'ALL'}

    all_urls = {}

    numbers = 0
    maxNumbers = 1
    browser = webdriver.Chrome(desired_capabilities=caps)
    # browser = webdriver.Firefox(capabilities=caps)
    time.sleep(0.5)
    # browser.get("https://wxtime.geekbang.org/column/article/408400")
    # browser.get("https://wxtime.geekbang.org/column/article/420021")
    # browser.get("https://wxtime.geekbang.org/column/article/455412")

    # browser.get("https://wxtime.geekbang.org/column/article/422975")
    browser.get("https://wxtime.geekbang.org")

    # browser.get("https://wxtime.geekbang.org/learn")
    # browser.get("https://wxtime.geekbang.org/?biz_benefit_record_id=1347072")
    # browser.get("https://wxtime.geekbang.org/course/intro/100015201?biz_benefit_record_id=1347072&tab=catalog")
    time.sleep(50)  # 等待页面加载, 扫码登录，手动点击到课程页
    # 点击大play
    find_and_play(browser)
    article_title = get_title_from_page(browser)
    print('article_title:', article_title)
    article_response(browser, article_title)
    while True:
        print("listening...{}.finish.".format(numbers))
        try:
            time.sleep(5)
            browser.find_element(By.CLASS_NAME, SMALL_PLAY_CLASS)  # 寻找播放按钮
            # TODO 不断扫描是否弹出播放按钮，代表播放结束了，需要： 停止录制 -> 点击下一页 -> 等待8秒 -> 保存文档 -> 开始录制 -> 点击播放，再不断监听播放按钮
            # 第一页完成播放，停止录制，跳转下一页
            if numbers > 0:
                next_page_btns = browser.find_elements(By.CLASS_NAME, NEXT_PAGE_CLASS)
                next_page_btn = next_page_btns[len(next_page_btns)-1]
                next_page_btn.click()  # 点击下一页
                time.sleep(8)
                current_url = browser.current_url
                if current_url in all_urls:
                    print("terminal url:" + current_url)
                    exit(0)
                else:
                    all_urls[current_url] = 1

                article_title = get_title_from_page(browser)
                print('article_title:', article_title)
                article_response(browser, article_title)

                # 寻找新页面播放按钮
                find_and_play(browser)
                # time.sleep(5)
        except Exception as e:
            continue
        numbers += 1
        if numbers > maxNumbers:
            print('stop')
            break
    print(all_urls)
    browser.close()
    browser.quit()
